package de.tum.team26.eistpoll.server;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * Persistant Class to verify users
 */
@PersistenceCapable
public class Logins {
	// Persistant Attributes
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
	@Persistent
	private String username;
	@Persistent
	private String password;
	
	/**
	 * Constructor
	 */
	public Logins(){
		this.id = new Long(1);
		this.username = "Prof";
		this.password = "new";
	}
	
	/**
	 * Load Persistant Data
	 */
	public void getPersistantData(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			Logins e = pm.getObjectById(Logins.class, id);
			this.username = e.username;
			this.password = e.password;
		} finally{
			pm.close();
		}
	}

	/**
	 * Set Persistant Data
	 */
	public void setPersistantData(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
        try{
        	pm.makePersistent(this);
        } finally{
        	pm.close();
        }

	}
	
	/**
	 * Check if User & Pass are valid
	 * @param username Username to be checked
	 * @param password Password to be checked
	 * @return true, if user & pass match DB entry, false otherwise
	 */
	public boolean checkUser(String username, String password){
		if ((this.username.equals(username)) && (this.password.equals(password))) return true;
		else return false;
	}
}
